package problem142;

//142.环形链表
//https://leetcode.cn/problems/linked-list-cycle-ii/

public class Solution {
    public ListNode detectCycle(ListNode head) {
        ListNode slow = head, fast = head;
        while(fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if(slow == fast) break;
        }
        if(fast == null || fast.next == null || slow != fast) return null;
        ListNode cur = head;
        while(cur != slow) {
            cur = cur.next;
            slow = slow.next;
        }
        return cur;
    }
}